
//require jquery-1.4.2
//require core.1.0
(function($, undefned){

$.Class("hg.base", {
	init : function(options){
		// this.option = options;
		$.extend(this, createOptionObject(options));
		this._handlerList = {};
	},
	
	bind : function(eventType, fn){
		var _this = this;
			handlers = this._handlerList
		
		if(arguments.length === 1){
			$.each(eventType, function(eventType, fn){
				_this.bind(eventType, fn);
			});
			return this;
		}
		
		eventType = eventType.split(" ");
		for(var i = 0, l = eventType.length; i < l; ++i){
			( handlers[ eventType[i] ] =  handlers[ eventType[i] ] || [] ).push(fn);
		}
		
		return this;
	},
	
	unbind : function(eventType, fn){
		var _this = this,
			handlers = this._handlerList[eventType],
			l = (this._handlerList[eventType] || []).length;

		switch(arguments.length){
			case 0:
				this._handlerList = {};
				break;
			
			case 1:
				if(typeof eventType == "string"){
					this._handlerList[eventType] = [];
				}else{
					$.each(eventType, function(eventType, fn){
						_this.unbind(eventType, fn);
					});
				}
				break;
			
			case 2:
				while(l--){
					if( handlers[l] === fn){
						handlers.splice(l, 1);
						return true;
					}
				}
				return false;
				break;
			default:
				//Throw ""
		}
		return this;
	},
	
	fire : function(eventType, data){
		var ret = true,
			handlers = ( this._handlerList[eventType] || [] ).concat(),
			l = handlers.length,
			inlineHandler = this["on" + eventType];
		data = data || {};
		
		if( inlineHandler && inlineHandler.call(this, data) === false){
			ret = false;
		}
		
		for(var i=0; i <l; ++i){
			if( handlers[i].call(this, data) === false){
				ret = false;
			}
		}
		
		return ret;
	}
});

var createOptionObject =  function(o){
	var Option = function(option){
			$.extend(this, option)
		};
		//getter = {};
		//setter = {};

	return {
		option : new Option(o),
		extendDefault : function(obj){
			$.extend(Option.prototype, obj);
		}
	}
};

hg.base.createOptionObject = createOptionObject;

})(jQuery);